AddCSLuaFile( "cl_init.lua" );
AddCSLuaFile( "shared.lua" );
include( "shared.lua" );

ENT.MaxHits = 1;

ENT.Sounds =
{
	
	Bounce =
	{
		
		Sound( "weapons/stunstick/stunstick_fleshhit1.wav" ),
		Sound( "weapons/stunstick/stunstick_fleshhit2.wav" )
		
	},
	
	Hit =
	{
		
		Sound( "ambient/energy/spark1.wav" ),
		Sound( "ambient/energy/spark2.wav" ),
		Sound( "ambient/energy/spark3.wav" ),
		Sound( "ambient/energy/spark4.wav" ),
		Sound( "ambient/energy/spark5.wav" ),
		Sound( "ambient/energy/spark6.wav" ),
		
	}
	
};

function ENT:Initialize()

	self.Entity:SetModel( "models/weapons/w_hasami.mdl" );
	self.Entity:PhysicsInit( SOLID_VPHYSICS );
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS );
	self.Entity:SetSolid( SOLID_VPHYSICS );

	local Physics = self.Entity:GetPhysicsObject( );

	if ValidEntity( Physics ) then

		Physics:Wake( );
		Physics:EnableGravity( false );

		Physics:ApplyForceCenter( self.Entity:GetAngles( ):Forward( ) * 4000 );

	end

	self.Trail = util.SpriteTrail( self.Entity, 0, Color( 0, 255, 2221, 225 ), false, 4, 0, 1, 1 / 2, "trails/plasma.vmt" );
	
	self.ShouldRemove = false;

end

function ENT:PhysicsCollide(Data,Physics)
	
	local ent = Data.HitEntity;
	
	if self.ShouldRemove then
	
		self:EmitSound( table.Random( self.Sounds.Hit ) );

		local fx = EffectData( );
		fx:SetOrigin( Data.HitPos );
		fx:SetNormal( Data.HitNormal );
		fx:SetMagnitude( 1.8 );
		fx:SetScale( 1 );
		fx:SetRadius( 5.5 );
		
		util.Effect( "sparks", fx, true, true );
		
		self.ShouldRemove = false;
		
		self.Entity:Remove( );
	
	else
	
		self.ShouldRemove = true;
		
		self:EmitSound( table.Random( self.Sounds.Bounce ) );
		
		if ent:IsNPC( ) || ent:IsPlayer( ) then
		
			self:EmitSound( table.Random( self.Sounds.Hit ) );
			
			self:PhysicsCollide( Data, Physics );
			
			return;
		
		end
		
		local Dir = Data.OurOldVelocity:GetNormal( );
		local Norm = Data.HitNormal;
		
		local TargetDir = Dir - 2 * ( Norm:DotProduct( Dir ) * Norm );
		
		self.Entity:SetAngles( TargetDir:Angle( ) );
		
		Physics:Sleep( );
		Physics:Wake( );
		
		Physics:ApplyForceCenter(TargetDir * 4000);
	
	end

	local fx = EffectData( ) ;
	fx:SetOrigin( Data.HitPos );
	fx:SetNormal( Data.HitNormal ) ;
		
	util.Effect( "AirboatGunImpact", fx );
	
	if ent:IsNPC( ) || ent:IsPlayer( ) then
	
		self:Electrify( ent, Data );
	
	else
	
		ent:TakeDamage( 15 + math.Rand( 5, 15 ), self.Entity:GetOwner( ), self.Entity );
	
	end
	
	util.Decal( "RedGlowFade", Data.HitPos - Data.HitNormal, Data.HitPos + Data.HitNormal );

end

function ENT:Electrify( ent, data )
	
	local owner = self:GetOwner( );
	local start = self:GetPos( );
	local endpos = data.HitPos - data.HitNormal * 2;
	
	local smin = Vector( -2, -2, -2 );
	local smax = smin * -1;
	
	local damage = 25;
	local force = 5;
	local dtype = DMG_PHYSGUN | DMG_ENERGYBEAM;
	
	owner:TraceHullAttack( start, endpos, smin, smax, damage, dtype, force, true );
	
end